package cn.gson.springboot.model.service.presaleservice;

import cn.gson.springboot.model.vo.presalevo.CategoryTree;
import cn.gson.springboot.model.dao.presaledao.BusinessInfoDao;
import cn.gson.springboot.model.dao.presaledao.DetailsDao;
import cn.gson.springboot.model.mapper.presalemapper.PsBusinessMapper;
import cn.gson.springboot.model.pjos.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商机业务类
 * */
@Service
@Transactional
public class PsBusinessService {
        @Autowired
        PsBusinessMapper psBusinessMapper;//注入商机Mapper
        @Autowired
        BusinessInfoDao businessInfoDao;//注入商机dao
        @Autowired
        DetailsDao detailsDao;//注入商机产品详情dao

        @Autowired
        PsBusinessDatailsTwoService businessDatailsTwoService;//注入商机详情2业务类

    /*查询当前用户商机列表*/
    public List<BusinessInfo> selectBusiness(int empId,int deptId,String businessName,Integer speedId){
        return psBusinessMapper.selectBusiness(empId,deptId,businessName,speedId);
    }
    /**特别关注商机*/
    public List<BusinessInfo> selectSpecialBusiness(int empId,int deptId,String businessName,Integer speedId){
        return psBusinessMapper.selectSpecialBusiness(empId,deptId,businessName,speedId);
    }
    /*查询所有的商机进度*/
    public List<SpeedInfo> allSpeed(){
        return psBusinessMapper.allSpeed();
    }

    /**查询当天最大的商机名称*/
    public String selectMaxBusinessName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = psBusinessMapper.selectMaxBusinessName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "M"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "M"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }
    /**查询该员工负责的客户*/
    public List<Customer> selectEmpCustomer(int empId, String cusName){
        return psBusinessMapper.selectEmpCustomer(empId,cusName);
    }
    /**查询客户对应的联系人*/
    public List<Culinkman> selectCustomerLinkman(int customerId){
        return psBusinessMapper.selectCustomerLinkman(customerId);
    }
    /**类型下的品牌*/
    public List<CategoryTree> category(){
        return  psBusinessMapper.category();
    }

    /**条件查询产品*/
    public List<Productside> productsList(String prName,Integer pcId,Integer pbId){
        return psBusinessMapper.productsList(prName,pcId,pbId);
    }
    /**新增或修改*/
    public int saveBusinessInfo(BusinessInfo businessInfo){
        businessInfo.setStatus("1");
        businessInfo.setUpdateTime(new Timestamp(new Date().getTime()));//更新时间
        if(businessInfo.getBusinesId() ==0 ){
            businessInfo.setBusinessTime(new Timestamp(new Date().getTime()));//创建时间
            businessInfo.setSpecialAttention(0);
        }else{
            BusinessInfo businessInfo1=queryBusiness(businessInfo.getBusinesId());
            businessInfo.setBusinessTime(businessInfo1.getBusinessTime());
            businessInfo.setSpecialAttention(businessInfo1.getSpecialAttention());
        }
        businessInfoDao.save(businessInfo);
        //先查询该商机下的产品
        List<Details> list=businessDatailsTwoService.detailsList(businessInfo.getBusinesId());
        //然后执行删除产品
        detailsDao.deleteAll(list);
        //新增或修改商机产品详情
        if(businessInfo.getDetailsByBusinesId().size()!=0){
            businessInfo.getDetailsByBusinesId().forEach(details -> {
                details.setBusinessInfoByBusinesId(businessInfo);
            });
            detailsDao.saveAll(businessInfo.getDetailsByBusinesId());
        }
        return businessInfo.getBusinesId();
    }
    /**根据商机id查询商机信息*/
    public BusinessInfo queryBusiness(int businesId){
        return psBusinessMapper.queryBusiness(businesId);
    }
    /* 查询该商机下的产品详情*/
    public List<Details> queryDetais(int businesId){
        return  psBusinessMapper.queryDetais(businesId);
    }


    //逻辑删除商机
    public void deleteBusiness(String business){
        JSONObject obj = (JSONObject) JSONObject.parse(business);
        List bList=obj.getObject("businessList",List.class);
        //转化成JSONArray
        JSONObject object=null;
        BusinessInfo businessInfo=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(bList));
        for (int i=0; i<json.size();i++){
            object = (JSONObject) json.get(i);//得到对象
            Integer businesId1=object.getInteger("businesId");
            businessInfo = businessInfoDao.findById(businesId1).get();
            businessInfo.setStatus("0");
            businessInfoDao.save(businessInfo);
        }
    }

    /**
     * 设置特别关注操作
     * @param businessinfo
     */
    public void setSpecial(String businessinfo) {
        JSONObject obj = (JSONObject) JSONObject.parse(businessinfo);
        Integer specialStauts = (Integer) obj.get("specialStatus");
        List list = obj.getObject("business",List.class);
        JSONArray jsonArray = JSONArray.parseArray(String.valueOf(list));
        JSONObject jsonObject = null;
        BusinessInfo business = null ;
        for (int i = 0; i<jsonArray.size();i++){
            jsonObject = (JSONObject) jsonArray.get(i);
            business = businessInfoDao .findById((Integer) jsonObject.get("businesId")).get();
            business.setUpdateTime(new Timestamp(new Date().getTime()));
            business.setSpecialAttention(specialStauts);
            businessInfoDao.save(business);
        }
    }

    /**
     * 新产品
     */
    public List<Zproduct> newProductInfo( String productName,Integer pcId, Integer pbId){
        return psBusinessMapper.newProductInfo(productName,pcId,pbId);
    }
}
